﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Gonte.Workflow.Tasks
{
    public class SetInstance
        : ITask
    {
        enum InstanceScope
        {
            Local,
            Global
        };

        #region ITask Members

        public TaskResult Execute(TaskContext context)
        {
            ExceptionThrower.ThrowWhenNullOrEmpty("Instance", _instance);
            ExceptionThrower.ThrowWhenNull("Value", _value);

            TaskResult result = _value.Execute(context);
            object value = result.Value;

            if (_scope == InstanceScope.Global)
            {
                context.Globals[_instance] = value;
            }
            else
            {
                context.SetLocal(_instance, value);
            }

            return null;
        }

        #endregion

        #region Properties

        public string Instance
        {
            get { return _instance; }
            set { _instance = value; }
        }

        public ITask Value
        {
            get { return _value; }
            set { _value = value; }
        }

        private InstanceScope Scope
        {
            get { return _scope; }
            set { _scope = value; }
        } 

        #endregion

        #region Fields

        private string _instance;
        private ITask _value;
        private InstanceScope _scope = InstanceScope.Global; 

        #endregion

    }
}
